package com.yx.learn.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yx.learn.domain.Company;
import com.yx.learn.domain.Department;
import com.yx.learn.service.CompanyService;
import com.yx.learn.service.DepartmentService;

@Controller("departmentAction")
@Scope("prototype")
public class DepartmentAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource
	DepartmentService departmentService;
	Department department = new Department();
	@Resource
	CompanyService companyService;
	Company company = new Company();

	private int id;
	private int pid;
	private String deptNo;
	private String deptName;
	private String companyName;
	private int companyID;
	private String memo;

	public void getAll() {
		String aListJson;
		try {
			aListJson = departmentService.getAlistJson(id).toString();
			this.toResponse(aListJson);
		} catch (Exception e) {
			// TODO: handle exception

			e.printStackTrace();
		}
	};

	public void save() {
		System.out.println("companyID="+companyID);
		try {
			if(pid!=1){
				company = companyService.findById(companyID);
				department.setPid(pid);
				department.setDeptName(deptName);
				department.setDeptNo(deptNo);
				department.setMemo(memo);
				department.setCompany(company);
				departmentService.saveDepartment(department);
				this.toResponse("{\"status\":\"ok\"}");
			}
		} catch (Exception e) {
			// TODO: handle exception

			e.printStackTrace();
		}finally{
			System.out.println(companyID);
		}
	};

	public void update() {
		try {
			if(id!=1){
				company = companyService.findById(companyID);
				department.setId(id);
				department.setPid(pid);
				department.setDeptNo(deptNo);
				department.setDeptName(deptName);
				department.setCompany(company);
				department.setMemo(memo);
				departmentService.updateDepartment(department);	
				this.toResponse("{\"status\":\"ok\"}");
			}else{
				this.toResponse("{\"status\":\"fail\" , \"message\":\"系统默认未分配职位，不能修改!\"}");
			}			
		} catch (Exception e) {
			// TODO: handle exception

			e.printStackTrace();
		}
	};

	public void delete() {
		try {
			if(id!=1){
				departmentService.deleteDepartment(departmentService.findById(id));
				this.toResponse("{\"status\":\"ok\"}");
			}else{
				this.toResponse("{\"status\":\"fail\" , \"message\":\"系统默认未分配职位，不能删除!\"}");
			}
		} catch (Exception e) {
			// TODO: handle exception

			e.printStackTrace();
		}
	};
	
	public void departmentsToCombobox(){
		try {
			String json = departmentService.departmentsToCombobox();
			this.toResponse(json);
		} catch (Exception e) {
			// TODO: handle exception

		}
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public String getDeptNo() {
		return deptNo;
	}

	public void setDeptNo(String deptNo) {
		this.deptNo = deptNo;
	}

	public String getDeptName() {
		return deptName;
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public int getCompanyID() {
		return companyID;
	}

	public void setCompanyID(int companyID) {
		this.companyID = companyID;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}
	
	

}
